package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.util.DateUtil;
import com.coding.dao.PoemDao;
import com.coding.dao.PoemPkDao;
import com.coding.pojo.PoemPkUser;
import com.google.gson.Gson;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Iterator;

/**
 * @auther wangchenghao
 * @date 2020/8/5
 **/
public class TestThreadImpl implements Runnable {
    PoemDao poemDao;
    PoemPkDao poemPkDao;
    int user_id;
    Socket socket;
    ServerSocket serverSocket;
    BufferedWriter rw;
    static Boolean flag;

    PoemPkUser poemPkUser;

    public TestThreadImpl(Socket socket, ServerSocket serverSocket, PoemDao poemDao, PoemPkDao poemPkDao, int user_id) {
        this.socket = socket;
        this.serverSocket = serverSocket;
        this.poemDao = poemDao;
        this.poemPkDao = poemPkDao;
        this.user_id = user_id;
    }

    @Override
    public void run() {
        try {

            if (PoempkImpl.socketArrayList.size() <= 2) {
                socket = serverSocket.accept();
                if (socket.isConnected()) {
                    PoempkImpl.socketArrayList.add(socket);

                }
                if (PoempkImpl.socketArrayList.size() == 1) {
                    poemPkUser = new PoemPkUser();
                    poemPkUser.setUser_one_id(user_id);
                    poemPkUser.setPoem_pk_time(DateUtil.getStringDate());
                    poemPkDao.insertSelective(poemPkUser);
                }
//                poemPkUser.setPoem_pk_time(DateUtil.getStringDate());
//                poemPkDao.insertSelective(poemPkUser);
                if (PoempkImpl.socketArrayList.size() >= 2 && socket.isConnected()) {
                    System.out.println("PoempkImpl.socketArrayList.size() = " + PoempkImpl.socketArrayList.size());
                    System.out.println("匹配成功");
//                    if (poemPkUser.getUser_one_id() == user_id) {
//                        PoempkImpl.socketArrayList.clear();
//                        if (serverSocket != null) {
//                            serverSocket.close();
//                        }
//                        if (socket != null) {
//                            socket.close();
//                        }
//                        return;
//                    }
                    PoemPkUser poemNewPk = poemPkDao.selectNewPoemPK();
                    if (poemNewPk.getUser_one_id()==user_id){
                        PoempkImpl.socketArrayList.clear();
                        if (serverSocket != null) {
                            serverSocket.close();
                        }
                        if (socket != null) {
                            socket.close();
                        }
                        return;
                    }
                    if (poemNewPk != null) {
                        poemPkDao.updateNewPoemPK(user_id, poemNewPk.getPoem_pk_id());
                        System.out.println("更新用户信息成功");
                    }

                    for (Iterator<Socket> it = PoempkImpl.socketArrayList.iterator(); it.hasNext(); ) {
                        Socket s = it.next();
                        try {
                            OutputStream os = s.getOutputStream();
                            rw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), "utf-8"));
//                        os.write((content + "\n").getBytes("utf-8"));
                            //json转化成字符串
                            BaseModel baseModel = new BaseModel();
                            baseModel.setSucceed("匹配成功", PoempkImpl.socketArrayList.size());
//                        JSONObject jsonObject= JSONObject.parseObject(poempk_string);
                            Gson gson = new Gson();
                            String json = gson.toJson(baseModel);
//                        rw.write(json+"\n");
                            //字符串转化byte数组
                            byte[] bytes = (json + "\n").getBytes("utf-8");
                            os.write(bytes);
                        } catch (SocketException e) {
                            e.printStackTrace();
                            it.remove();
                            System.out.println(PoempkImpl.socketArrayList);
                        }
                    }
                    PoempkImpl.socketArrayList.clear();
                    addnewthread();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addnewthread() {
//        //关闭
//        for (Iterator<Socket> it = PoempkImpl.socketArrayList.iterator(); it.hasNext(); ) {
//            Socket s = it.next();
//            try {
//                if
//                s.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        PoempkImpl.socketArrayList.clear();
        for (int i = 0; i < 2; i++) {
            try {
                Socket user = serverSocket.accept();
                PoempkImpl.pkList.add(user);
                if (user.isConnected()) {
                    System.out.println(user.getInetAddress() + "进入房间成功");
                }

                new Thread(new ServerimplThread(user, poemDao, serverSocket)).start();
                if (PoempkImpl.pkList.size() >= 2) {
                    System.out.println("两位进入房间");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
